package com.example.hasee.zhcd.Activities.TongJi;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Activities.ChuanWu.AtWorkActivity;
import com.example.hasee.zhcd.Adapters.CgsDetailAdapter;
import com.example.hasee.zhcd.Adapters.SchlDetailAdapter;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.DateTimePickDialogUtil;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import static com.example.hasee.zhcd.R.id.btn_query;
import static com.example.hasee.zhcd.R.id.end_date;
import static com.example.hasee.zhcd.R.id.schl_detail_list;
import static com.example.hasee.zhcd.R.id.start_date;

/**
 * Created by hasee on 2018/5/28.
 */

public class CgsDetailActivity extends BaseActivity {
    private HashMap<String, String> map = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private TextView title;
    private ImageView back;
    private ImageView home;
    //    private Button btn_query;
//    private EditText start_date;
//    private EditText end_date;
    private ListView cgs_detail_list;
    //    private int mYear;
//    private int mMonth;
//    private int mDay;
    private String startString;
    private String endString;
    private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    private CgsDetailAdapter adapter;
    private String strShip;
    private String strCGS;

    @Override
    public int contentViewId() {
        return R.layout.activity_cgs_detail;
    }

    @Override
    public void set() {
        title.setText("船公司查询详情");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);
//        start_date.setOnClickListener(this);
//        end_date.setOnClickListener(this);
//        btn_query.setOnClickListener(this);
//        final Calendar c = Calendar.getInstance();
//        mYear = c.get(Calendar.YEAR);
//        mMonth = c.get(Calendar.MONTH) - 1;
//        mDay = c.get(Calendar.DAY_OF_MONTH);
//        start_date.setText(getOldDate(-30));
//        end_date.setText(getTime());
//        startString = start_date.getText().toString();
//        endString = end_date.getText().toString();
        getDetaillist();
    }

    @Override
    public void init() {
        strShip = getIntent().getExtras().getString("strShip");
        startString = getIntent().getExtras().getString("strKSSJ");
        endString = getIntent().getExtras().getString("strJSSJ");
        strCGS = getIntent().getExtras().getString("strCGS");
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
//        btn_query = (Button) findViewById(btn_query);
//        start_date = (EditText) findViewById(start_date);
//        end_date = (EditText) findViewById(end_date);
        cgs_detail_list = (ListView) findViewById(R.id.cgs_detail_list);
    }

    public String getTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    public static String getOldDate(int distanceDay) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }

    private void getDetaillist() {
        ProgressDialogUtils.createProgressDialog(CgsDetailActivity.this, "正在加载列表...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strShip", strShip);
        map.put("strKSSJ", startString);
        map.put("strJSSJ", endString);
        map.put("strCgs", strCGS);
        httpUtils.get("ZHTJ_CGSZHCX_Detail", map, "ZHTJ_CGSZHCX_Detail");
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "ZHTJ_CGSZHCX_Detail":
                Log.e("ZHTJ_CGSZHCX_Detail", msg.getMsg());
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        list.clear();
                        cgs_detail_list.setVisibility(View.VISIBLE);
                        for (int i = 0; i < data.length(); i++) {
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put("fshipname", data.getJSONObject(i).getString("fshipname"));
                            map.put("fhc", data.getJSONObject(i).getString("fhc"));
                            map.put("fjd", data.getJSONObject(i).getString("fjd"));
                            map.put("fkbsj", data.getJSONObject(i).getString("fkbsj"));
                            map.put("fberth", data.getJSONObject(i).getString("fberth"));
                            map.put("fwgsj", data.getJSONObject(i).getString("fwgsj"));
                            map.put("flgsj", data.getJSONObject(i).getString("flgsj"));
                            map.put("flx", data.getJSONObject(i).getString("flx"));
                            map.put("fzx", data.getJSONObject(i).getString("fzx"));
                            map.put("fhm", data.getJSONObject(i).getString("fhm"));
                            map.put("fsl", data.getJSONObject(i).getString("fsl"));
                            map.put("ffl", data.getJSONObject(i).getString("ffl"));
                            map.put("fdlf", data.getJSONObject(i).getString("fdlf"));
                            list.add(map);
                        }
                        adapter = new CgsDetailAdapter(CgsDetailActivity.this, list);
                        cgs_detail_list.setAdapter(adapter);
                    } else {
                        cgs_detail_list.setVisibility(View.GONE);
                        Toast.makeText(CgsDetailActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
//            case start_date:
//                String t1 = "";
//                if (start_date.getText().toString().equals("")) {
//                    t1 = getTime();
//                } else {
//                    t1 = start_date.getText().toString();
//                }
//                DateTimePickDialogUtil dialog1 = new DateTimePickDialogUtil(this,
//                        t1);
//                dialog1.dateTimePicKDialog(start_date);
//                break;
//            case end_date:
//                String t2 = "";
//                if (end_date.getText().toString().equals("")) {
//                    t2 = getTime();
//                } else {
//                    t2 = end_date.getText().toString();
//                }
//                DateTimePickDialogUtil dialog2 = new DateTimePickDialogUtil(this,
//                        t2);
//                dialog2.dateTimePicKDialog(end_date);
//                break;
//            case btn_query:
//                list.clear();
//                startString = start_date.getText().toString();
//                endString = end_date.getText().toString();
//                View errorview = inputCheck();
//                if (errorview != null) {
//                    errorview.setFocusable(true);
//                    errorview.requestFocus();
//                    errorview.setFocusableInTouchMode(true);
//                    return;
//                } else {
//                    getDetaillist();
//                }
//                break;
        }
    }

//    private View inputCheck() {
//        if (startString.length() == 0) {
//            return start_date;
//        } else if (endString.length() == 0) {
//            return end_date;
//        }
//        return null;
//    }


}
